<?php
$sql = "select tag from ".$this->config["table_prefix"]."pages where ".
       "latest = 'Y' and LEFT(supertag,7)!='comment' and supertag !='export'";
$pages = $this->LoadAll($sql);
//if (isset($_REQUEST['export_sites'])) {

   //$exports = explode('$', $_REQUEST['export_sites']);

   for ($i=0;$i<=count($pages);$i++) {
      $pagex = $pages[$i];
      //echo $pagex[tag];
      $seiten[] = $pagex[tag];
   }

   $css = implode("\n", file("./themes/".$this->config['theme']."/css/".$this->config['default_css']));
   $zip_dir = "";
   require_once("./lib/zip.php");
   $zipfile = new zipfile();
   //$zipfile -> add_dir($zip_dir);
   $zipname = "export.zip";

   foreach($seiten as $seite) {
      if (!$seite == "") {
         
         $this->SetPage($this->LoadPage(trim($seite)));

         $oldconfig = $this->config['base_url'];
         $this->config['base_url'] = "./";
         $body = $this->format($this->page['body']);
	 $body = preg_replace('/(\.\/[^\\"\s]*)"/','$1.html"',$body);
	 //echo "<blockquote>".$body."</blockquote>";
         $this->config['base_url'] = $oldconfig;

         $content = "<html>\n<head>\n<title>".trim($seite)."</title>\n";

         $content .= "<style type=\"text/css\">\n";
         $content .= $css."\n";
         $content .= "</style>\n";

         $content .= "</head>\n<body>\n<div class=\"page export\">\n";
         $content .= $body."\n";
         $content .= "</div>\n</body>\n</html>\n";

	 $position = strripos(strtolower(trim($seite)),"/");
	 if($position) {
		 $dir = substr(trim($seite),0,$position);
		 mkdir("./var/export/".$dir,0x777,true);
	 }
	 
         $fp = fopen("./var/export/".strtolower(trim($seite)).".html", "w");
         fwrite($fp, $content);
         fclose($fp);

         $exportierte .= $seite.'<br />';

         $filedata = implode("", file("./var/export/".strtolower(trim($seite)).".html"));
         $zipfile -> add_file($filedata, $zip_dir.trim($seite).".html");
      }
   }

   header("Content-type: application/zip");
   header("Content-disposition: attachment; filename=$zipname");

   echo $zipfile -> file();
   exit;
?>
